package cn.ljy.uem.web.action.backstage.school;

import cn.ljy.authority.web.controller.backstage.BaseController;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.uem.model.dto.SchoolSectionResource;
import cn.ljy.uem.service.*;
import cn.ljy.uem.utils.file.UemFileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api(tags = "教师个人课程管理")
@Validated //加了后@NoNull之类的注解才能生效，如果只是@valid注解则不需要加上
@RestController
@RequestMapping(value = "/api/backstage/school/schoolTeacherCourse")
public class SchoolTeacherCourseController extends BaseController {
    @Autowired
    private SchoolTeacherService schoolTeacherService;
    @Autowired
    private SchoolTeacherCourseService schoolTeacherCourseService;
    @Autowired
    private SchoolCourseService schoolCourseService;
    @Autowired
    private SchoolCourseSectionResourceService schoolCourseSectionResourceService;
    @Autowired
    private SchoolCourseResourceService schoolCourseResourceService;
    @Autowired
    private UemFileUploadUtil uemFileUploadUtil;

    @ApiOperation(value = "读取教师个人拥有的课程列表")
    @GetMapping("/teacher/session/courses")
    public ResponseData CoursesForSession() {
        return ResponseData.success(schoolTeacherCourseService.getCourseAndCourseOfTeachingtaskOfLoginTeacher());
    }

    @ApiOperation(value = "上传课程的封面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象"),
            @ApiImplicitParam(name = "courseId", value = "课程主键", defaultValue = "1")
    })
    @PostMapping("/uploadCourseCover")
    public ResponseData uploadImage(@NotNull(message = "请选择上传文件") MultipartFile file, @NotNull(message = "请选择要上传的课程") Long courseId) {
        // 上传课程封面，并获取上传成功后的图片网址
        return ResponseData.success(schoolTeacherCourseService.uploadCoverOfCourse(courseId,file));
    }


    @ApiOperation(value = "给课程章节添加资源")
    @PostMapping("/section/resources")
    protected ResponseData UploadingChapterResources(@Valid @RequestBody SchoolSectionResource schoolSectionResource) {
        ResponseData result=ResponseData.error("资源添加失败!");
        if (schoolCourseSectionResourceService.uploadSectionResources(schoolSectionResource.getSectionId(), schoolSectionResource.getResourceIds())) {
            result=ResponseData.success("资源添加成功!");
        }
        return result;
    }


}
